package com.xbongbong.workflow.service.operator.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.enums.LogicalSymbolEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.pojo.workflow.WorkflowDataPojo;
import com.xbongbong.paas.pojo.workflow.WorkflowExplainPojo;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.util.FormDataUtil;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.enums.workflow.WorkflowValueTypeEnum;
import com.xbongbong.workflow.domain.entity.WorkflowNodeEntity;
import com.xbongbong.workflow.pojo.WorkflowOperationsPojo;
import com.xbongbong.workflow.pojo.WorkflowTransferPOJO;
import com.xbongbong.workflow.pojo.nodes.UpdateRecordNode;
import com.xbongbong.workflow.service.operator.WorkflowOperatorFieldHandler;
import com.xbongbong.workflow.service.operator.WorkflowOperatorService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2021/06/18 15:09
 */
@Service("workflowFixedValueOperatorServiceImpl")
public class WorkflowFixedValueOperatorServiceImpl implements WorkflowOperatorService {

    private static final Logger LOGGER = LoggerFactory.getLogger(WorkflowFixedValueOperatorServiceImpl.class);

    @Resource
    private Map<Integer, WorkflowOperatorFieldHandler> workflowOperatorFieldMap;


    @Override
    public Integer type() {
        return WorkflowValueTypeEnum.FIXED_VALUE.getType();
    }


    @Override
    public Object operatorByInsertNodeSourceData(WorkflowNodeEntity workflowNodeEntity, FieldAttrEntity fieldAttrEntity, FieldAttrEntity targetField, Map<Long, PaasFormDataEntityExt> formDataMap,
                           Map<Long, JSONObject> dataMap, WorkflowOperationsPojo workflowOperationsPojo, WorkflowDataPojo workflowDataPojo,
                                                 WorkflowTransferPOJO workflowTransferPojo, WorkflowExplainPojo workflowExplainPojo) throws XbbException {

        try {
            WorkflowOperatorFieldHandler workflowOperatorFieldHandler = workflowOperatorFieldMap.get(targetField.getFieldType());
            return workflowOperatorFieldHandler.handlerFixedValue(targetField, workflowDataPojo, workflowOperationsPojo, workflowNodeEntity);
        }  catch (XbbException e) {
            throw e;
        } catch (Exception e) {
            LOGGER.error("WorkflowFixedValueOperatorServiceImpl.operatorByInsertNodeSourceData error", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
    }

    @Override
    public Object operatorByUpdateNode(WorkflowNodeEntity workflowNodeEntity, FieldAttrEntity fieldAttrEntity, FieldAttrEntity targetField, Map<Long, PaasFormDataEntityExt> formDataMap, Map<Long, JSONObject> dataMap, WorkflowOperationsPojo workflowOperationsPojo, WorkflowDataPojo workflowDataPojo, WorkflowTransferPOJO workflowTransferPojo, WorkflowExplainPojo workflowExplainPojo) throws XbbException {
        UpdateRecordNode updateRecordNode = JSON.parseObject(workflowNodeEntity.getConfigData(), UpdateRecordNode.class);
        // 获取更新的数据
        JSONObject data = dataMap.get(updateRecordNode.getWorkflowNodeId());
        Object value = operatorByInsertNodeSourceData(workflowNodeEntity, fieldAttrEntity, targetField,
                null, null, workflowOperationsPojo, workflowDataPojo, workflowTransferPojo, workflowExplainPojo);
        if (!Objects.equals(LogicalSymbolEnum.EQUAL.getSymbol(), workflowOperationsPojo.getSymbol())) {
            return FormDataUtil.count(targetField, data, targetField.getAttr(), value, workflowOperationsPojo.getSymbol());
        }
        return value;
    }
}
